package com.jdwx.opengl;

import android.content.Intent;
import android.opengl.GLSurfaceView;
import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.jdwx.opengl.test.Name;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.GL_FLOAT;
import static android.opengl.GLES20.GL_LINES;
import static android.opengl.GLES20.GL_POINTS;
import static android.opengl.GLES20.GL_TRIANGLES;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glDrawArrays;
import static android.opengl.GLES20.glEnableVertexAttribArray;
import static android.opengl.GLES20.glGetAttribLocation;
import static android.opengl.GLES20.glGetUniformLocation;
import static android.opengl.GLES20.glUniform4f;
import static android.opengl.GLES20.glUseProgram;
import static android.opengl.GLES20.glVertexAttribPointer;
import static android.opengl.GLES20.glViewport;

public class MainActivity extends AppCompatActivity implements GLSurfaceView.Renderer {

    private static final int POSITION_COMPONENT_COUNT = 2;
    private GLSurfaceView gl;
    private static final int BYTES_PER_FLOAT = 4;
    private FloatBuffer vertextData;
    private String vertextShaderSource;
    private String fragmentShaderSource;
    private int vertextShader;
    private int fragmentShader;
    private int program;

    final String U_COLOR = "u_Color";
    int uColorLocation;
    final String A_POSITION = "a_Position";
    int aPositionLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();

                startActivity( new Intent( MainActivity.this, Detail.class));
            }
        });


        //把数据复制到本地内存块
        vertextData = ByteBuffer.allocateDirect(AirHockeyRenderer.tableVerticesWithTriangles.length * BYTES_PER_FLOAT
        ).order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        vertextData.put( AirHockeyRenderer.tableVerticesWithTriangles );

        vertextShaderSource = TextResourceReader.readTextFileFromResource(
                this,R.raw.simple_vertex_shader
        );
        fragmentShaderSource = TextResourceReader.readTextFileFromResource(
                this,R.raw.simple_fragment_shader
        );

        gl = findViewById(R.id.gl);
        gl.setEGLContextClientVersion( 2 );

        gl.setRenderer( this );

        gl.requestRender();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        if( gl!=null ){
//            gl.onResume();
//        }
    }

    @Override
    protected void onPause() {
        super.onPause();
//        if( gl!=null ){
//            gl.onPause();
//        }
    }

    @Override
    public void onSurfaceCreated(GL10 gl1, EGLConfig config) {
        glClearColor(1.0f,0.0f, 0.0f,0.0f);

        vertextShader = ShaderHelper.compileVertexShader( vertextShaderSource );
        fragmentShader = ShaderHelper.compileFragmentShader( fragmentShaderSource );



        program = ShaderHelper.linkProgram( vertextShader,fragmentShader);
        ShaderHelper.validataProgram( program );
        glUseProgram( program );


        uColorLocation = glGetUniformLocation( program, U_COLOR );

        aPositionLocation = glGetAttribLocation( program, A_POSITION );


        vertextData.position( 0 );

        glVertexAttribPointer(aPositionLocation,POSITION_COMPONENT_COUNT,GL_FLOAT
        ,false,0, vertextData);


        glEnableVertexAttribArray( aPositionLocation );
        Log.d("ss","onSurfaceCreated");

    }

    @Override
    public void onSurfaceChanged(GL10 gl1, int width, int height) {
        glViewport(0,0,width,height);
        Log.d("ss","onSurfaceChanged");
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        Log.d("ss","onDrawFrame");
        glClear(GL_COLOR_BUFFER_BIT);
        glUniform4f(uColorLocation, 1.0f,1.0f,1.0f,1.0f);
        glDrawArrays(GL_TRIANGLES,0, 6 );

        glUniform4f(uColorLocation, 1.0f,0.0f,0.0f,1.0f);
        glDrawArrays(GL_LINES,6, 2 );


        glUniform4f(uColorLocation, 0.0f,0.0f,1.0f,1.0f);
        glDrawArrays(GL_POINTS,8, 1 );

        glUniform4f(uColorLocation, 1.0f,0.0f,0.0f,1.0f);
        glDrawArrays(GL_POINTS,9, 1 );
    }

}
